/* 
 * File:   MainWindow.h
 * Author: jcrada
 *
 * Created on April 11, 2009, 6:53 PM
 */

#ifndef _MAINWINDOW_H
#define	_MAINWINDOW_H
#include <QtGui/QMainWindow>

#include "ui/ui_Inspector.h"
#include "ui/ui_MainWindow.h"
#include "Inspector.h"

#include "SimulationThread.h"
#include "QGLRendererThread.h"
#include "Creature.h"
#include "EvolutionOfCreatures.h"
#include "ExperimentThread.h"

#include <QtGui/QDockWidget>
#include <string>


namespace jcrada {

    class MainWindow : public QMainWindow {
        Q_OBJECT
    public slots:

        virtual void changedMouseMode( int index );
        virtual void changedSensitivity( double sensitivity );

        virtual void changedTimesteps( int tps );
        virtual void changedFrameRate( int fps );
        virtual void changedCameraFrameRate(int fps);
        virtual void changedSimulationMode( int index );
        virtual void changedGravity( double gravity );
        virtual void changedIterations( int iterations );

        virtual void clickStep( );
        virtual void clickPlayPause( );
        virtual void changedSimulationSpeed( int speed );
        virtual void activateShortcuts( bool active );
        virtual void pressedShortcut( int key );


        virtual void changedRender( bool value );
        virtual void changedShowAxis( bool value );
        virtual void changedShowCenterOfMass( bool value );
        virtual void changedShowConnectionPoints( bool value );
        virtual void changedShowBoundingSpheres( bool value );
        virtual void changedFollowCreature( bool value );
        virtual void changedSolidDraw( bool value );
        virtual void changedBlackAndWhite(bool value);
        virtual void changedRenderGround(bool value);

        virtual void changedCurrentTab( int tab );

        virtual void changedCreature( int index );
        virtual void clickCreateCreature( );
        virtual void clickAddCreatureToWorld( );

        virtual void changedXmlCreature(int index);
        virtual void clickXmlFromClipboard( );
        virtual void clickXmlToClipboard( );

        virtual void changedBehaviorEnabled( bool value );
        virtual void changedBehaviorOnGround( bool value );

        virtual void clickResetEvolution( );
        virtual void clickSetEvolution( );

        virtual void setPlayIcon( );
        virtual void setPauseIcon( );

        virtual void sendUiMessage( const QString& message );

        virtual void changedBodyPartSelection( );

        virtual void clickClearForces( );
        virtual void clickApplyCentralForce( );
        virtual void clickApplyCentralTorque( );
        virtual void clickApplyImpulseForce( );
        virtual void clickApplyImpulseTorque( );

        virtual void clickAddExperiment();
        virtual void clickRemoveExperiment();
        virtual void clickUpdateCurrentExperiment();
        virtual void clickRestartExperiments();

    private:
        Inspector* _inspector;

        Ui::MainWindow* _ui_main_window;
        Ui::Inspector* _ui_inspector;

        SimulationThread* _simulation_thread;
        QGLRendererThread* _renderer_thread;

        Creature* _creature;
        EvolutionOfCreatures* _evolution_of_creatures;

        bool _toggle_color_message;
        bool _activate_shortcuts;
        int _body_part_selection_index;

        btTypedConstraint* _hook;
        std::vector<ExperimentThread*>* _experiment_threads;
    protected:

        enum eSimulationMode {
            SM_FREE = 0, SM_EVOLUTION, SM_EXPERIMENTS, SM_INSTANT_REPLAY
        };

        enum eForceType {
            FT_NONE = -1, FT_CENTRAL_FORCE, FT_CENTRAL_TORQUE,
            FT_IMPULSE_FORCE, FT_IMPULSE_TORQUE
        };

        virtual void updateControls( );
        virtual void setupUi( );

        virtual void setupControl( );
        virtual void setupEvolution( );
        virtual void setupBodyParts( );

        virtual void play( );
        virtual void pause( );

        virtual void setSimulationMode( eSimulationMode mode );
        virtual void clickApplyForce( eForceType type );
        
        virtual void createCreature( Creature* creature );
        virtual void removeCreature( );
        virtual void loadCreatureDetails( const Creature& creature );
        virtual void showBodyDetails( const Creature& creature, const BodyPart& body_part );

        virtual btTypedConstraint* getHook() const{
            return this->_hook;
        }
        
        virtual void setHook(btTypedConstraint* hook){
            this->_hook = hook;
        }

        virtual void setCreature( Creature* creature ) {
            this->_creature = creature;
        }

        virtual void setSimulationThread( SimulationThread& thread ) {
            this->_simulation_thread = &thread;
        }

        virtual void setEvolutionOfCreatures( EvolutionOfCreatures* evolution ) {
            this->_evolution_of_creatures = evolution;
        }
    public:
        static int main( int argc, char** argv );
        MainWindow( QWidget* parent = NULL );
        virtual ~MainWindow( );

        virtual void closeEvent( QCloseEvent* event );

        virtual Ui::MainWindow& getUiMainWindow( ) const {
            return *this->_ui_main_window;
        }

        virtual Inspector& getInspector( ) const {
            return *this->_inspector;
        }

        virtual Ui::Inspector& getUiInspector( ) const {
            return *this->_ui_inspector;
        }

        virtual SimulationThread& getSimulationThread( ) const {
            return *this->_simulation_thread;
        }

        virtual QGLRendererThread& getRendererThread( )const {
            return *this->_renderer_thread;
        }

        virtual Creature* getCreature( ) const {
            return this->_creature;
        }

        virtual EvolutionOfCreatures* getEvolutionOfCreatures( ) const {
            return this->_evolution_of_creatures;
        }
    };
}

#endif	/* _MAINWINDOW_H */

